package com.arcbbs.controller;

import com.arcbbs.annotation.GlobalInterceptor;
import com.arcbbs.entity.ForumComment;
import com.arcbbs.entity.ForumCommentQuery;
import com.arcbbs.entity.LikeRecord;
import com.arcbbs.entity.SessionWebUserDto;
import com.arcbbs.exception.BaseException;
import com.arcbbs.mapper.LikeRecordMapper;
import com.arcbbs.result.Result;
import com.arcbbs.service.ForumCommentService;
import com.arcbbs.service.LikeRecordService;
import com.arcbbs.utils.StringTools;
import com.arcbbs.utils.SysCacheUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/comment")
public class ForumCommentController {
    @Resource
    private ForumCommentService forumCommentService;

    @RequestMapping("/loadcomment")
    @GlobalInterceptor(checkparams = true)
    public Result loadComment(HttpSession httpSession, String articleId, Integer pageNo, Integer orderType) {
        final String order_type = "good_count desc,comment_id asc";
        final String order_type1 = "comment_id desc";

        if (!SysCacheUtils.getSysSetting().getSystemSettingCommentDto().getCommentOpen()) {
            throw new BaseException("用户未开启评论");
        }
        ForumCommentQuery forumCommentQuery = new ForumCommentQuery();
        forumCommentQuery.setArticleId(articleId);
        String orderBy = orderType == null || orderType == 0 ? order_type : order_type1;
        forumCommentQuery.setOrderBy("top_type desc," + orderBy);
        forumCommentQuery.setPageNo(pageNo);
        SessionWebUserDto sessionWebUserDto = (SessionWebUserDto) httpSession.getAttribute("sessionKey");
        if (sessionWebUserDto != null) {
            forumCommentQuery.setQueryLikeType(true);
            forumCommentQuery.setCurrentUserId(sessionWebUserDto.getUserId());
        } else {
            forumCommentQuery.setStatus(1);
        }
        forumCommentQuery.setPageSize(50);
        forumCommentQuery.setpCommentId(0);
        forumCommentQuery.setLoadChildren(true);
        return Result.success(forumCommentService.findListByParam(forumCommentQuery));

    }

    @Resource
    private LikeRecordService likeRecordService;
@Resource
private LikeRecordMapper likeRecordMapper;
    @RequestMapping("/dolike")
    @GlobalInterceptor(checkLogin = true)
    public Result dolike(HttpSession httpSession, Integer commentId) {
        SessionWebUserDto sessionWebUserDto = (SessionWebUserDto) httpSession.getAttribute("sessionKey");

        String objectId = String.valueOf(commentId);
        likeRecordService.dolike(objectId, sessionWebUserDto.getUserId(), sessionWebUserDto.getNickName(), 1);
        LikeRecord likeRecord =likeRecordMapper.getbyobjectiduseridoptype(objectId,sessionWebUserDto.getUserId(),1);
        ForumComment comment=forumCommentService.getForumCommentById(commentId);
comment.setLikeType(likeRecord == null ? 0:1);
        return Result.success(comment);
    }
    @RequestMapping("/changetoptype")
    @GlobalInterceptor(checkLogin = true)
    public Result changetoptype(HttpSession httpSession,Integer commentId,Integer topType){
        SessionWebUserDto sessionWebUserDto = (SessionWebUserDto) httpSession.getAttribute("sessionKey");
        forumCommentService.changetoptype(sessionWebUserDto.getUserId(),commentId,topType);
        return Result.success();
    }
    @RequestMapping("/postcomment")
    @GlobalInterceptor(checkLogin = true)
    public Result postcomment(HttpSession httpSession, String articleId, Integer pCommentId, String content, MultipartFile image,
                              String replyUserId){

        if(StringTools.isEmpty(content)){
            throw new BaseException("评论和内容必须全部填写");

        }
        //获取用户信息
        SessionWebUserDto sessionWebUserDto= (SessionWebUserDto) httpSession.getAttribute("sessionKey");
       //转码content
        ForumComment forumComment=new ForumComment();
        content=StringTools.eccapeHtml(content);
        forumComment.setUserId(sessionWebUserDto.getUserId());
        forumComment.setNickName(sessionWebUserDto.getNickName());
        forumComment.setUserIpAddress(sessionWebUserDto.getProvince());
        forumComment.setpCommentId(pCommentId);
        forumComment.setArticleId(articleId);
        forumComment.setContent(content);
        forumComment.setReplyUserId(replyUserId);
        forumComment.setTopType(0);
        //评论
        forumCommentService.postcomment(forumComment,image);
        if(pCommentId!=0){
            ForumCommentQuery forumCommentQuery=new ForumCommentQuery();
            forumCommentQuery.setArticleId(articleId);
            forumCommentQuery.setpCommentId(pCommentId);
            forumCommentQuery.setOrderBy("comment_id asc");
            List<ForumComment> children=forumCommentService.findListByParam(forumCommentQuery);
            return  Result.success(children);
        }
        return Result.success(forumComment);

    }


}
